package com.ctl.jvm.jmh;

/**
 * <p>Title: d</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.ctl.com</p>
 *
 * @author ctl
 * @version 1.1
 * @date 2024-07-04 10:35
 */

import org.openjdk.jmh.annotations.*;

import java.util.Arrays;
import java.util.stream.IntStream;

/**
 * IntegerSumTests
 * created at 2021/4/25
 *
 * @author weny
 * @since 1.0.0
 */
public class IntegerSumTests {

    // Implementation using stream summingInt
    @Benchmark
    public int streamSummingInt(Params params) {
        return Arrays.stream(params.items).sum();
    }

    // Implementation using forEach
    @Benchmark
    public int forEachPlus(Params params) {
        int res = 0;

        for (int item : params.items) {
            res += item;
        }

        return res;
    }

    // Define benchmarks parameters with @State
    @State(Scope.Benchmark)
    public static class Params {
        // Run with given size parameters of
//        @Param({"1000", "10000", "100000", "1000000"})
        @Param({"10000", "100000", "1000000", "10000000"})
        public int size;

        // Items to run benchmark on
        public int[] items;

        // Setup test data, will be run once and will not affect our results
        @Setup
        public void setUp() {
            items = IntStream.range(0, size).toArray();
        }
    }

}